﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 测量大师
{
    class measuring_adjustment
    {
        //闭合导线 两个已知点
        public static DataTable BH2Point(DataTable dataTable , DataTable resultTable , out double DeltaAngle , out double Ssum , out double DeltasumX , out double DeltasumY , int judge)
        {
            int rows = dataTable.Rows.Count;
            #region 通过两点坐标反算坐标方位角
            resultTable.Rows[0][3] = calculator.XYtoAZ((double)dataTable.Rows[0][9], (double)dataTable.Rows[1][9], (double)dataTable.Rows[0][10], (double)dataTable.Rows[1][10]);
            resultTable.Rows[1][3] = calculator.CalAz(resultTable.Rows[0][3].ToString(), resultTable.Rows[0][1].ToString(),judge);
            #endregion

            #region 利用转折角计算改正后的转折角
            string[] Angle = new string[rows - 2];
            for (int i = 2; i < rows; i++)
            {
                Angle[i - 2] = dataTable.Rows[i][1].ToString();
            }
            string[] AngelGai;//改正数没有在表格中显示
            string[] GaihouAngle = calculator.Corangle(Angle, out AngelGai , out DeltaAngle);
            for (int i = 0; i < rows - 2; i++)
            {
                resultTable.Rows[i + 2][2] = GaihouAngle[i];
            }
            //resultTable.Rows[2][1] = dataTable.Rows[rows - 1][1];
            //resultTable.Rows[2][2] = dataTable.Rows[rows - 1][2];

            #endregion

            #region 利用已知坐标方位角和转折角计算坐标方位角
            for (int i = 2; i < rows-1; i++)
            {
                resultTable.Rows[i][3] = calculator.CalAz(resultTable.Rows[i - 1][3].ToString(), resultTable.Rows[i][2].ToString(),judge);
            }
            #endregion
            
            #region DX DY的计算
            for (int i = 2; i < rows; i++)
            {
                resultTable.Rows[i][5] = calculator.CalDelatX((double)resultTable.Rows[i - 1][4], resultTable.Rows[i - 1][3].ToString());
                resultTable.Rows[i][6] = calculator.CalDelatY((double)resultTable.Rows[i - 1][4], resultTable.Rows[i - 1][3].ToString());
            }
            #endregion

            #region Dx Dy改正数的计算
            double[] Dx = new double[rows - 2];
            double[] Dy = new double[rows - 2];
            double[] S = new double[rows - 2];
            double[] DxGai, DyGai;
            for (int i = 0; i < rows - 2; i++)
            {
                S[i] = (double)dataTable.Rows[i+1][4];
            }
            for (int i = 0; i < rows - 2; i++)
            {
                Dx[i] = (double)resultTable.Rows[i + 2][5];
                Dy[i] = (double)resultTable.Rows[i + 2][6];
            }
            double[] GaiHouDx = calculator.XYPingCha(Dx, S, out DxGai , out Ssum ,out DeltasumX );
            double[] GaiHouDy = calculator.XYPingCha(Dy, S, out DyGai , out Ssum ,out DeltasumY );
            for (int i = 0; i < rows - 2; i++)
            {
                resultTable.Rows[i + 2][7] = GaiHouDx[i];
                resultTable.Rows[i + 2][8] = GaiHouDy[i];
            }
            #endregion

            #region X Y的计算
            for (int i = 2; i < rows; i++)
            {
                resultTable.Rows[i][9] = (double)resultTable.Rows[i - 1][9] + (double)resultTable.Rows[i][7];
                resultTable.Rows[i][10] = (double)resultTable.Rows[i - 1][10] + (double)resultTable.Rows[i][8];
            }

            #endregion
            #region 修正
            //对最后的X，Y进行修正
            resultTable.Rows[rows - 1][9] = (double)resultTable.Rows[1][9];
            resultTable.Rows[rows - 1][10] = (double)resultTable.Rows[1][10];
            //DX与DY的修正
            resultTable.Rows[rows - 1][7] = (double)resultTable.Rows[rows - 1][9]- (double)resultTable.Rows[rows - 2][9];
            resultTable.Rows[rows - 1][8] = (double)resultTable.Rows[rows - 1][10] - (double)resultTable.Rows[rows - 2][10];
            #endregion
            return resultTable;

        }

        //闭合导线 一个已知点一个方位角
        public static DataTable BH1Pointl1AZ(DataTable dataTable , DataTable resultTable , out Double DeltaAngle , out double Ssum , out double DeltasumX , out double DeltasumY, int judge )
        {
            
            int rows = dataTable.Rows.Count;

            #region          通过转折角计算改正后转折角
            string[] Angle = new string[rows-1];
            for (int i = 1; i < rows; i++)
            {
                Angle[i - 1] = resultTable.Rows[i][1].ToString();
            }
            string[] AngelGai;//改正数没有在表格中显示
            string[] GaihouAngle = calculator.Corangle(Angle, out AngelGai ,out DeltaAngle);
            for (int i = 0; i < rows-1; i++)
            {
                resultTable.Rows[i + 1][2] = GaihouAngle[i]; 
            }
            #endregion

            #region 通过转折角和方位角推算方位角
            resultTable.Rows[0][3] = dataTable.Rows[0][3];
            for (int i = 1; i < rows; i++)
            {
                resultTable.Rows[i][3] = calculator.CalAz(resultTable.Rows[i - 1][3].ToString(), resultTable.Rows[i][2].ToString(),judge); 
            }
            #endregion

            #region Dx Dy的计算
            for (int i = 1; i < rows; i++)
            {
                resultTable.Rows[i][5] = calculator.CalDelatX((double)resultTable.Rows[i - 1][4], resultTable.Rows[i - 1][3].ToString());
                resultTable.Rows[i][6] = calculator.CalDelatY((double)resultTable.Rows[i - 1][4], resultTable.Rows[i - 1][3].ToString());
            }
            #endregion

            //从这里之前都要检查表格的正确性， 确定了用同一份表格
            #region Dx Dy改正数的计算
            double[] Dx = new double[rows - 1];
            double[] Dy = new double[rows - 1];
            double[] S = new double[rows - 1];
            double[] DxGai, DyGai;
            for (int i = 0; i < rows-1; i++)
            {
                S[i] = (double)resultTable.Rows[i][4];
            }
            for (int i = 0; i < rows-1; i++)
            {
                Dx[i] = (double)resultTable.Rows[i + 1][5];
                Dy[i] = (double)resultTable.Rows[i + 1][6];
            }
            double[] GaiHouDx = calculator.XYPingCha(Dx, S, out DxGai , out Ssum ,out DeltasumX);
            double[] GaiHouDy = calculator.XYPingCha(Dy, S, out DyGai, out Ssum, out DeltasumY);
            for (int i = 0; i < rows-1; i++)
            {
                resultTable.Rows[i + 1][7] = GaiHouDx[i];
                resultTable.Rows[i + 1][8] = GaiHouDy[i];
            }
            #endregion

            #region X,Y的计算
            for (int i = 1; i < rows; i++)
            {
                resultTable.Rows[i][9] = (double)resultTable.Rows[i - 1][9] + (double)resultTable.Rows[i][7];
                resultTable.Rows[i][10] = (double)resultTable.Rows[i - 1][10] + (double)resultTable.Rows[i][8];
            }
            #endregion

            #region 修正
            //对最后的X，Y进行修正
            resultTable.Rows[rows - 1][9] = (double)resultTable.Rows[0][9];
            resultTable.Rows[rows - 1][10] = (double)resultTable.Rows[0][10];
            //DX与DY的修正
            resultTable.Rows[rows - 1][7] = (double)resultTable.Rows[rows - 1][9] - (double)resultTable.Rows[rows - 2][9];
            resultTable.Rows[rows - 1][8] = (double)resultTable.Rows[rows - 1][10] - (double)resultTable.Rows[rows - 2][10];
            #endregion

            return resultTable;
        }

        //支导线
        public static DataTable Zhi(DataTable dataTable , DataTable resultTable,int judge)
        {
            int rows = dataTable.Rows.Count;
            #region 利用坐标反算坐标方位角
            resultTable.Rows[0][2] = calculator.XYtoAZ((double)dataTable.Rows[0][6], (double)dataTable.Rows[1][6], (double)dataTable.Rows[0][7], (double)dataTable.Rows[1][7]);
            #endregion

            #region 利用已知方位角和转折角推算方位角
            for (int i = 1; i < rows-1; i++)
            {
                resultTable.Rows[i][2] = calculator.CalAz(resultTable.Rows[i - 1][2].ToString() , resultTable.Rows[i][1].ToString(),judge);
            }
            #endregion

            #region 利用方位角和边长推算Dx和DY
            for (int i = 2; i < rows; i++)
            {
               
                 resultTable.Rows[i][4] = calculator.CalDelatX((double)resultTable.Rows[i - 1][3], resultTable.Rows[i - 1][2].ToString());
                 resultTable.Rows[i][5] = calculator.CalDelatY((double)resultTable.Rows[i - 1][3], resultTable.Rows[i - 1][2].ToString());
            }
            #endregion

            # region X Y的计算
            for (int i = 2; i < rows; i++)
            {
                resultTable.Rows[i][6] = (double)resultTable.Rows[i - 1][6] + (double)resultTable.Rows[i][4];
                resultTable.Rows[i][7] = (double)resultTable.Rows[i - 1][7] + (double)resultTable.Rows[i][5];
            }
            #endregion

            return resultTable;

        }

        //双定向附合导线
        public static DataTable SDXFH(DataTable dataTable , DataTable resultTable ,out int DeltaAngle , out double Ssum , out double DeltaX , out double DeltaY,int judge)
        {
            int rows = dataTable.Rows.Count;

            #region 通过坐标反算坐标方位角
            resultTable.Rows[0][2] = calculator.XYtoAZ((double)resultTable.Rows[0][9], (double)resultTable.Rows[1][9], (double)resultTable.Rows[0][10], (double)resultTable.Rows[1][10]);
            resultTable.Rows[0][6] = resultTable.Rows[0][2];
            resultTable.Rows[rows-2][6] = calculator.XYtoAZ((double)resultTable.Rows[rows-2][9], (double)resultTable.Rows[rows-1][9], (double)resultTable.Rows[rows-2][10], (double)resultTable.Rows[rows-1][10]);
            #endregion

            #region 通过坐标方位角与转折角推算坐标方位角
            for (int i = 1; i < rows-1; i++)
            {
                resultTable.Rows[i][2] = calculator.CalAz(resultTable.Rows[i - 1][2].ToString(), resultTable.Rows[i][1].ToString(),judge);
            }
            #endregion

            #region DX DY的计算
            for (int i = 2; i < rows-1; i++)
            {
                resultTable.Rows[i][4] = calculator.CalDelatX((double)resultTable.Rows[i - 1][3], resultTable.Rows[i - 1][2].ToString());
                resultTable.Rows[i][5] = calculator.CalDelatY((double)resultTable.Rows[i - 1][3], resultTable.Rows[i - 1][2].ToString());
            }
            #endregion

            #region 计算改正后的方位角
            string[] AZ = new string[rows - 3];
            
            for (int i = 0; i < AZ.Length; i++)
            {
                AZ[i] = resultTable.Rows[i + 1][2].ToString();
            }

            string[] PAZ = calculator.FHPingChaAZ(AZ, resultTable.Rows[rows - 2][2].ToString(), resultTable.Rows[rows - 2][6].ToString() , out DeltaAngle);

            for (int i = 0; i < PAZ.Length ;  i++)
            {
                resultTable.Rows[i + 1][6] = PAZ[i];
            }
            #endregion

            #region 改正后DX DY的计算
            
            double[] S = new double[rows - 3];
            double[] DX = new double[rows - 3];
            double[] DY = new double[rows - 3];
            for (int i = 0; i < S.Length; i++)
            {
                S[i] = (double)resultTable.Rows[i + 1][3];
                DX[i] = (double)resultTable.Rows[i + 2][4];
                DY[i] = (double)resultTable.Rows[i + 2][5];
            }
            double EndX = (double)resultTable.Rows[1][9];
            double EndY = (double)resultTable.Rows[1][10];
            for (int i = 2; i < rows-1; i++)
            {
                EndX += (double)resultTable.Rows[i][4];
                EndY += (double)resultTable.Rows[i][5];
            }
            double EndTX = (double)resultTable.Rows[rows - 2][9];
            double EndTY = (double)resultTable.Rows[rows - 2][10];

            double[] PDX = calculator.FHPingChaDXDY(DX, S, EndX, EndTX , out Ssum , out DeltaX);
            double[] PDY = calculator.FHPingChaDXDY(DY, S, EndY, EndTY , out Ssum , out DeltaY);

            for (int i = 2; i < rows-1; i++)
            {
                resultTable.Rows[i][7] = PDX[i - 2];
                resultTable.Rows[i][8] = PDY[i - 2];
            }

            #endregion

            #region X Y的计算
            for (int i = 0; i < PDX.Length - 1; i++)
            {
                resultTable.Rows[i + 2][9] = PDX[i] + (double)resultTable.Rows[i + 1][9];
                resultTable.Rows[i + 2][10] = PDY[i] + (double)resultTable.Rows[i + 1][10];
            }
            //修正乘除后精度缺失的部分 此块内容必须放在X Y计算的部分，因为如果不算出X Y，没有办法完全平差
            resultTable.Rows[rows - 2][7] = (double)resultTable.Rows[rows - 2][9] - (double)resultTable.Rows[rows - 3][9];
            resultTable.Rows[rows - 2][8] = (double)resultTable.Rows[rows - 2][10] - (double)resultTable.Rows[rows - 3][10];
            #endregion

            return resultTable;
        }

        //单定向附合导线
        public static DataTable DDXFH(DataTable dataTable , DataTable resultTable , out double Ssum , out double DeltasumX , out double DeltasumY,int judge )
        {
            int rows = dataTable.Rows.Count;
            #region 通过坐标反算坐标方位角
            resultTable.Rows[0][2] = calculator.XYtoAZ((double)resultTable.Rows[0][9], (double)resultTable.Rows[1][9], (double)resultTable.Rows[0][10], (double)resultTable.Rows[1][10]);
            #endregion
            #region 根据转折角和方位角推算方位角
            for (int i = 1; i < rows-1; i++)
            {
                resultTable.Rows[i][2] = calculator.CalAz(resultTable.Rows[i - 1][2].ToString(), resultTable.Rows[i][1].ToString(),judge);
            }
            #endregion

            #region DX DY的计算
            for (int i = 2; i < rows; i++)
            {
                resultTable.Rows[i][4] = calculator.CalDelatX((double)resultTable.Rows[i - 1][3], resultTable.Rows[i - 1][2].ToString());
                resultTable.Rows[i][5] = calculator.CalDelatY((double)resultTable.Rows[i - 1][3], resultTable.Rows[i - 1][2].ToString());
            }
            #endregion

            #region 改正后DX DY的计算
            double[] S = new double[rows - 2];
            double[] DX = new double[rows - 2];
            double[] DY = new double[rows - 2];
            for (int i = 0; i < S.Length; i++)
            {
                S[i] = (double)resultTable.Rows[i + 1][3];
                DX[i] = (double)resultTable.Rows[i + 2][4];
                DY[i] = (double)resultTable.Rows[i + 2][5];
            }
            double EndX = (double)resultTable.Rows[1][9];
            double EndY = (double)resultTable.Rows[1][10];
            for (int i = 2; i < rows ; i++)
            {
                EndX += (double)resultTable.Rows[i][4];
                EndY += (double)resultTable.Rows[i][5];
            }
            double EndTX = (double)resultTable.Rows[rows - 1][9];
            double EndTY = (double)resultTable.Rows[rows - 1][10];

            double[] PDX = calculator.FHPingChaDXDY(DX, S, EndX, EndTX , out Ssum , out DeltasumX);
            double[] PDY = calculator.FHPingChaDXDY(DY, S, EndY, EndTY , out Ssum , out DeltasumY);

            for (int i = 2; i < rows ; i++)
            {
                resultTable.Rows[i][7] = PDX[i - 2];
                resultTable.Rows[i][8] = PDY[i - 2];
            }
            #endregion

            #region X Y计算
            for (int i = 0; i < PDX.Length - 1; i++)
            {
                resultTable.Rows[i + 2][9] = PDX[i] + (double)resultTable.Rows[i + 1][9];
                resultTable.Rows[i + 2][10] = PDY[i] + (double)resultTable.Rows[i + 1][10];
            }
            //修正乘除后精度缺失的部分 此块内容必须放在X Y计算的部分，因为如果不算出X Y，没有办法完全平差
            resultTable.Rows[rows - 1][7] = (double)resultTable.Rows[rows - 1][9] - (double)resultTable.Rows[rows - 2][9];
            resultTable.Rows[rows - 1][8] = (double)resultTable.Rows[rows - 1][10] - (double)resultTable.Rows[rows - 2][10];
            #endregion


            return resultTable;
        }

        //无定向附合导线
        public static DataTable WDXFH(DataTable dataTable, DataTable resultTable, string AssAZ, int judge)
        {

            int rows = dataTable.Rows.Count;
            dataTable.Rows[0][2] = AssAZ;
            resultTable.Rows[0][2] = AssAZ;
            #region 添加改正后边长列
            DataColumn 改正后边长 = new DataColumn("改正后边长", typeof(double));
            DataColumn 改正后边长1 = new DataColumn("改正后边长", typeof(double));
            dataTable.Columns.Add(改正后边长);
            resultTable.Columns.Add(改正后边长1);
            #endregion

            #region 通过转折角和方位角推算方位角
            for (int i = 1; i < rows-1; i++)
            {
                resultTable.Rows[i][2] = calculator.CalAz(resultTable.Rows[i - 1][2].ToString(), resultTable.Rows[i][1].ToString(),judge);
            }
            #endregion

            #region 假定DX DY的计算
            for (int i = 1; i < rows ; i++)
            {
                resultTable.Rows[i][4] = calculator.CalDelatX((double)resultTable.Rows[i - 1][3], resultTable.Rows[i - 1][2].ToString());
                resultTable.Rows[i][5] = calculator.CalDelatY((double)resultTable.Rows[i - 1][3], resultTable.Rows[i - 1][2].ToString());

            }
            #endregion

            #region 计算坐标变换参数
            double AssX = (double)resultTable.Rows[0][9];
            double AssY = (double)resultTable.Rows[0][10];
            for (int i = 1; i < rows; i++)
            {
                AssX += (double)resultTable.Rows[i][4];
                AssY += (double)resultTable.Rows[i][5];
            }
            double S = AssX *AssX + AssY *AssY;
            //计算坐标变换参数 u 和 t 的公式
            double u = ((double)resultTable.Rows[rows - 1][9] - (double)resultTable.Rows[0][9]) * AssX / S + ((double)resultTable.Rows[rows - 1][10] - (double)resultTable.Rows[0][10]) * AssY / S;
            double t = ((double)resultTable.Rows[rows - 1][10] - (double)resultTable.Rows[0][10]) * AssX / S - ((double)resultTable.Rows[rows - 1][9] - (double)resultTable.Rows[0][9]) * AssY / S;
            #endregion

            #region X Y的计算
            double[] AssXi = new double[rows - 2];
            double[] AssYi = new double[rows - 2];
            for (int i = 0; i < AssXi.Length; i++)
            {
                AssXi[i] = (double)resultTable.Rows[i][9] + (double)resultTable.Rows[i + 1][4];
                resultTable.Rows[i + 1][9] = AssXi[i];
                AssYi[i] = (double)resultTable.Rows[i][10] + (double)resultTable.Rows[i + 1][5];
                resultTable.Rows[i + 1][10] = AssYi[i];

            }
            for (int i = 1; i < rows-1; i++)
            {
                resultTable.Rows[i][9] = (double)resultTable.Rows[0][9] + AssXi[i-1] * u - t * AssYi[i-1];
                resultTable.Rows[i][10] = (double)resultTable.Rows[0][10] + AssXi[i-1] * t + u * AssYi[i-1];
            }
            #endregion

            #region 改正后DX DY的计算
            #endregion

            #region 改正后方位角的计算
            for (int i = 1; i < rows; i++)
            {
                resultTable.Rows[i][7] = (double)resultTable.Rows[i][9] - (double)resultTable.Rows[i - 1][9];
                resultTable.Rows[i][8] = (double)resultTable.Rows[i][10] - (double)resultTable.Rows[i - 1][10];
            }
            #endregion

            #region 改正后边长的计算
            #endregion





            return resultTable;
        }

    }
}
